/***
 * These unit tests are more like integration tests -
 *  the reason being - it relies on document.local and its interface which is very limited
 */

/* Clear loalStorage */
if (window && window.localStorage && window.localStorage.clear)
    window.localStorage.clear();

module("js-simple-storage::local");

/* Check that interface contracts for window.storage.local exists */
test("interface contract tests", function() {
    expect(5);
    equals(typeof window.storage.local, 'object', 'object exists');
    equals(typeof window.storage.local.getItem, 'function', 'function getItem exists');
    equals(typeof window.storage.local.setItem, 'function', 'function setItem exists');
    equals(typeof window.storage.local.removeItem, 'function', 'function removeItem exists');
    equals(typeof window.storage.local.clear, 'function', 'function clear exists');
});

test('getItem', function() {
    // set up a context
    window.localStorage.setItem('a', 'a');
    window.localStorage.setItem(1, '1');
    
    expect(4);
    equals(window.storage.local.getItem('a'), 'a', 'getItem(string) returns correct value');
    equals(window.storage.local.getItem(1), '1', 'getItem(number) returns correct value');
    equals(window.storage.local.getItem(), null, 'getItem() returns null');
    equals(window.storage.local.getItem(null), null, 'getItem(null) returns null');
    
    // clean up by removing the locals
    window.localStorage.removeItem('a');
    window.localStorage.removeItem(1);
});

test('removeItem', function() {
    // set up a context
    window.localStorage.setItem('a', 'a');
    window.localStorage.setItem(1, '1');
    
    expect(2);
    window.storage.local.removeItem('a');
    window.storage.local.removeItem(1);
    
    ok(window.localStorage.getItem('a') == null, 'local \'a\' removed successfully');
    ok(window.localStorage.getItem(1) == null, 'local 1 removed successfully');
});

test("keys", function() {
    expect(1);
    
    // set up
    window.storage.setItem('a', 'a');
    window.storage.setItem('aaa', 'a');
    
    ok(window.storage.local.keys().length == 2, "correct number of keys returned");
    
    // clean up by removing the cookies
    window.localStorage.removeItem('a');
    window.localStorage.removeItem('aaa');
});
